﻿@{
    Layout = "~/Views/Shared/_SysPage.cshtml";

    ConfigDefinition configDefinition = ViewBag.ConfigDefinition;
    string[] attributes = configDefinition.Attributes;
    var attribute = attributes == null ? "特征" : attributes[0];
    var attribute1 = attributes == null ? "特征1" : attributes[1];
    var attribute2 = attributes == null ? "特征2" : attributes[2];
    var hasAttribute = attribute != "Undefined";
    var hasAttribute1 = attribute1 != "Undefined";
    var hasAttribute2 = attribute2 != "Undefined";


    ShowHideOrNone authorizationOption = configDefinition.AuthorizationOption;
    var hasAuthorization = authorizationOption == ShowHideOrNone.Show;
    var hasAuthorizationMark = configDefinition.HasPermissionMark;
    var hasConsumers = configDefinition.HasConsumers;

    bool isManager = configDefinition.IsManager;
    bool isProducer = configDefinition.IsProducer;
    var masterId = configDefinition.SubType;
}

<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <div class="select-list">
                <ul>
                    <li>
                        <input id="masterId" col="OptionString" type="text" hidden="hidden" />
                    </li>
                    <li>
                        父项信息：<input id="masterInfo" col="MasterInfo" type="text" readonly="readonly" />
                    </li>
                    <li>
                        状态：<span id="status" col="JudgementOption"></span>
                    </li>
                    <li>
                        键、特征或值：<input id="mark" col="Mark" type="text" />
                    </li>
                    <li>
                        名称或描述：<input id="text" col="Text" type="text" />
                    </li>

                    <li class="select-time">
                        <label>创建时间： </label>
                        <input id="startCreationTime" col="StartDateTime" type="text" class="time-input" placeholder="开始时间" />
                        <span>-</span>
                        <input id="endCreationTime" col="EndDateTime" type="text" class="time-input" placeholder="结束时间" />
                    </li>
                    <li>
                        <a id="btnSearch" class="btn btn-primary btn-sm" onclick="searchGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="btn-group-sm" id="toolbar">
            @if (isProducer)
            {
                <a id="btnAdd" class="btn btn-success" onclick="showAddEditDialog(true)"><i class="fa fa-plus"></i> 新增</a>
                <a id="btnEdit" class="btn btn-primary disabled" onclick="showAddEditDialog(false)"><i class="fa fa-edit"></i> 修改</a>
                <a id="btnEditValue" class="btn btn-primary disabled" onclick="showEditValueDialog(false)"><i class="fa fa-edit"></i> 编辑值</a>
                <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteSelected()"><i class="fa fa-remove"></i> 删除</a>
                <a id="btnCopy" class="btn btn-success disabled" onclick="copy()"><i class="fa fa-edit"></i> 复制</a>
            }
            <a id="btnDisplay" class="btn btn-primary disabled" onclick="showDisplayDialog()"><i class="fa fa-search"></i> 查看</a>

            @if (isManager & configDefinition.HasConsumers)
            {
                <a id="btnGrantAsConsumer" class="btn btn-success" onclick="goToGrantAsConsumer()"><i class="fa fa-plus"></i> 授权消费者</a>
            }

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<script type="text/javascript">

    var masterInfo = ys.request("masterInfo");
    var masterId = ys.request("masterId");

    $(function () {
        $("#masterInfo").val(masterInfo);
        $("#masterId").val(masterId);
        initGrid();
        $("#gridTable").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table", function () {
            var selectedRows = $("#gridTable").bootstrapTable("getSelections");
            if ($('#btnEditValue')) {
                $('#btnEditValue').toggleClass('disabled', selectedRows.length != 1);
            }
            if ($('#btnCopy')) {
                $('#btnCopy').toggleClass('disabled', selectedRows.length != 1);
            }
            if ($('#btnDisplay')) {
                $('#btnDisplay').toggleClass('disabled', selectedRows.length != 1);
            }
        });

        laydate.render({ elem: '#startCreationTime', format: 'yyyy-MM-dd' });
        laydate.render({ elem: '#endCreationTime', format: 'yyyy-MM-dd' });
        $("#status").ysComboBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });

    });

    function initGrid() {
        var queryUrl = '@Url.Content("~/Sys/ConfigItem/GetPagedManageDtosJson")'+'?masterId=@masterId';
        $('#gridTable').ysTable({
            url: queryUrl,
            sortName: 'Sequence',
            sortOrder: 'Asc',
            toolbar: '#toolbar',
            columns: [
                { checkbox: true, visible: true },
                {
                    field: 'Key', width: '120px', title: '键', sortable: true},
                { field: 'Attribute', sortable: true, width: '120px', title: '@(attribute)', visible:'@(hasAttribute)'=='True' },
                { field: 'Attribute1', sortable: true, width: '120px', title: '@(attribute1)', visible: '@(hasAttribute1)' =='True' },
                { field: 'Attribute2', sortable: true, width: '120px', title: '@(attribute2)', visible: '@(hasAttribute2)' == 'True' },

                {
                    field: 'Authorization', width: '80px', sortable: true, title: '权限', visible: '@(hasAuthorization)' == 'True',
                    formatter: function (value, row, index) {
                        if (row.Authorization == "@((int)Authorization.Anonymous)") {
                            return '<span class="badge badge-danger">' + "@Authorization.Anonymous.GetDescription()" + '</span>';
                        }
                        else if (row.Authorization == "@((int)Authorization.AnyOne)") {
                            return '<span class="badge badge-success">' + "@Authorization.AnyOne.GetDescription()" + '</span>';
                        }
                        else if (row.Authorization == "@((int)Authorization.AnyUser)") {
                            return '<span class="badge badge-info">' + "@Authorization.AnyUser.GetDescription()" + '</span>';
                        }
                        else if (row.Authorization == "@((int)Authorization.AnyMachine)") {
                            return '<span class="badge badge-primary">' + "@Authorization.AnyMachine.GetDescription()" + '</span>';
                        }
                        else
                        {
                            return '<span class="badge badge-warning">' + "@Authorization.TobePermitted.GetDescription()" + '</span>';
                        }
                    }
                },

                { field: 'PermissionMark', sortable: true, width: '80px', title: '授权标识', visible: '@(hasAuthorizationMark)' == 'True' },
                { field: 'Name', title: '名称' },
                { field: 'Description', title: '描述' },
                { field: 'ConsumerNum', sortable: true, width: '80px', title: '使用者数', visible: '@(hasConsumers)' == 'True' },
                {
                    field: 'IsDefault', title: '默认', sortable: true, width: '40px',
                    formatter: function (value, row, index) {
                        //return ys.getValueByKey(ys.getJson(@Html.Raw(typeof(YesOrNo).EnumToDictionaryJson())), value);
                        if (row.IsDefault == "@((int)YesOrNo.Yes)") {
                            return '<span class="badge badge-primary">' + "@YesOrNo.Yes.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@YesOrNo.No.GetDescription()" + '</span>';
                        }
                    }
                },
                {
                    field: 'Style', title: '样式', sortable: true, width: '40px',
                    formatter: function (value, row, index) {
                        if (value) {
                            return '<span class="badge badge-' + value + '">&nbsp;&nbsp;&nbsp;&nbsp;</span>';
                        }
                    }
                },
                { field: 'Sequence', title: '排序码', sortable: true, width: '60px', },
                {
                    field: 'Status', title: '状态', width: '40px',sortable: true,formatter: function (value, row, index) {
                        if (row.Status == "@((int)StatusType.Enabled)") {
                            return '<span class="badge badge-primary">' + "@StatusType.Enabled.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@StatusType.Disabled.GetDescription()" + '</span>';
                        }
                    }
                },
                {
                    field: 'ModificationTime', title: '最后修改时间', sortable: true, width: '150px',
                    formatter:function (value, row, index) {
                        return ys.formatDate(value, "yyyy-MM-dd HH:mm:ss");
                    }
                },
                { field: 'Id', title: 'Id', visible: false, width: '100px',},
                {
                    title: '操作',
                    align: 'center', width: '100px',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs" href="#" onclick="setAsDefault(\'' + row.Id + '\')"><i class="fa fa-list-ul"></i>设为默认</a>');
                        return actions.join('');
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').ysTable('getPagination', params);
                var queryString = $('#searchDiv').getWebControls(pagination);
                return queryString;
            }
        })
    }

    function searchGrid() {
        $('#gridTable').ysTable('search');
        resetToolbarStatus();
    }

    function showAddEditDialog(add) {
       var id = '';
        if (add) {
        }
        else {
            var selectedRows = $("#gridTable").bootstrapTable("getSelections");
            if (!ys.checkRowEdit(selectedRows)) {
                    return;
                }
                id = selectedRows[0].Id;
        }

        ys.openDialog({
            title: id == '' ? "新增配置子项" : "修改配置子项",
            content: '@Url.Content("~/Sys/ConfigItem/AddEditModal")' + '?id=' + id + '&masterId=' + masterId + '&masterInfo=' + masterInfo,
            callback: function (index, layero) {
                var frame = window[layero.find('iframe')[0]['name']];
                frame.saveForm(index);
            }
        });
    }

    function showDisplayDialog() {
       var id = '';
       var selectedRows = $("#gridTable").bootstrapTable("getSelections");
       if (!ys.checkRowEdit(selectedRows)) {
                    return;
       }
       id = selectedRows[0].Id;

        ys.openDialog({
            title: "查看配置子项",
            btn:['关闭'],
            content: '@Url.Content("~/Sys/ConfigItem/AddEditModal")' + '?id=' + id + '&masterId=' + masterId + '&masterInfo=' + masterInfo,
            callback: function (index, layero) {
                var frame = window[layero.find('iframe')[0]['name']];
                frame.leaveForm(index);
            }
        });
    }


    function showEditValueDialog() {
        var id = '';
        var selectedRows = $("#gridTable").bootstrapTable("getSelections");
        if (!ys.checkRowEdit(selectedRows)) {
            return;
        }
        else {
            id = selectedRows[0].Id;
            type = selectedRows[0].Type;
        }

        var w = '100%';
        var h = '100%';
        ys.openDialog({
            title: "编辑值-" + selectedRows[0].Name ,
            content: '@Url.Content("~/Sys/ConfigItem/AddEditModal")' + '?index=EditValueModal&masterId='+masterId+'&id=' + id,
            width: w,
            height:h,
            btn: ['保存', '关闭'],
            callback: function (index, layero) {
                var frame = window[layero.find('iframe')[0]['name']];
                frame.saveForm(index);
            }
        });
    }


    function deleteSelected() {
        var selectedRows = $("#gridTable").bootstrapTable("getSelections");
        if (ys.checkRowDelete(selectedRows)) {
            ys.confirm("确认要删除选中的" + selectedRows.length + "条数据吗？", function () {
                var ids = ys.getIds(selectedRows);
                ys.ajax({
                    url: '@Url.Content("~/Sys/ConfigItem/DeleteSelected")' + '?ids=' + ids,
                    type: "post",
                    success: function (rst) {
                        if (rst.Flag == 1) {
                            ys.msgSuccess(rst.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(rst.Message);
                        }
                    }
                });
            });
        }
    }

    function copy() {
        var selectedRows = $("#gridTable").bootstrapTable("getSelections");
        var id = selectedRows[0].Id;
           ys.ajax({
                    url: '@Url.Content("~/sys/ConfigItem/copy")' + '?id=' + id,
                    type: "post",
                    success: function (rst) {
                        if (rst.Flag == 1) {
                            ys.msgSuccess(rst.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(rst.Message);
                        }
                    }
           });
    }

    function setAsDefault(id) {
           ys.ajax({
                    url: '@Url.Content("~/sys/ConfigItem/SetAsdefault")' + '?id=' + id,
                    type: "post",
                    success: function (rst) {
                        if (rst.Flag == 1) {
                            ys.msgSuccess(rst.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(rst.Message);
                        }
                    }
           });
    }

    function goToGrantAsConsumer() {
        var objId ='@(configDefinition.SubType)';
        var index = @((int)PermissionType.GrantAsConsumerForConfigItem);
        var url = '@Url.Content("~/sys/Permission/ManageConfig")' + '?index=' + index+'&objId='+objId;
        createMenuItem(url, "权限管理-授权配置消费者");
    }



</script>
